package com.fzrskj.cloud.free.sys.service;

import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.enums.PriceTypeEnum;
import com.fzrskj.cloud.free.core.enums.ProductStatusEnum;
import com.fzrskj.cloud.free.core.enums.ProductTypeEnum;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.tool.YJsonTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.dic.bean.DicParamItem;
import com.fzrskj.cloud.free.dic.utils.DicParamItemUtils;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.fzrskj.cloud.free.dic.utils.DicPriceDiyUtils;
import com.fzrskj.cloud.free.sys.bean.*;
import com.fzrskj.cloud.free.sys.utils.*;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysProductService extends CObject {

    /**
     * 获取产品列表.
     *
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String getCustomerProductList(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        String productClass = CJsonTool.getStrValue(p, "productClass", "");
        String subClass = CJsonTool.getStrValue(p, "subClass", "");
        String productCode = CJsonTool.getStrValue(p, "productCode", "");
        int page = CJsonTool.getIntValue(p, "page", 1);
        int pageSize = CJsonTool.getIntValue(p, "pageSize", 9);
        String productName = CJsonTool.getStrValue(p, "productName", "");
//        int pageSize = CJsonTool.getIntValue(p, "pageSize", CConsts.PAGE_SIZE);
        Integer recommend = CJsonTool.getIntValue(p, "recommend", -1);

        List<SysProductView> list;
        SysProductUtils sysProductUtils = new SysProductUtils();
        Object subClassInfo = new JSONObject();
        if (!StrTool.isBlankStr(subClass) || !StrTool.isBlankStr(productCode)) {
            if (!StrTool.isBlankStr(subClass)) {
                SysProductDiycatUtils diycatUtils = new SysProductDiycatUtils();
                Map<String, Object> catCdt = new HashedMap();
                catCdt.put("categoryCode", subClass);
                subClassInfo = diycatUtils.getSysProductDiycat(catCdt);
            }
//            list = sysProductUtils.getProductList(ProductStatusEnum.NORMAL.getStatus(), productClass, subClass, productCode, recommend, page, pageSize);
            list = sysProductUtils.getProductList(ProductStatusEnum.NORMAL.getStatus(), productClass, subClass, productCode, recommend, productName, page, pageSize);
        } else {
            list = sysProductUtils.getProductList(ProductStatusEnum.NORMAL.getStatus(), productClass, recommend, page, pageSize);
        }

        if (ProductTypeEnum.V_HOST.equals(productClass) && null != list && CloudContextUtils.getAgentId() > 0) {
            list.forEach(row -> {
                SysProduct sysProduct = sysProductUtils.getProductAppointAgent(productClass, row.getProductCode(), CloudContextUtils.DEF_AGENT_ID);
                if (null != sysProduct) {
                    com.alibaba.fastjson.JSONObject productParamOld = com.alibaba.fastjson.JSONObject.parseObject(sysProduct.getProductParam());
                    if (null == productParamOld) {
                        productParamOld = new com.alibaba.fastjson.JSONObject();
                    }
                    com.alibaba.fastjson.JSONObject productParam = com.alibaba.fastjson.JSONObject.parseObject(row.getProductParam());
                    if (null != productParam) {
                        productParamOld.putAll(productParam);
                    }
                    row.setProductParam(com.alibaba.fastjson.JSONObject.toJSONString(productParamOld));
                }
            });
        }

        //获取自定义产品 1个月，3个月，6个月，1年 购买价格
        if (ProductTypeEnum.DIY.equals(productClass) && null != list) {
            list.forEach(row -> {
                com.alibaba.fastjson.JSONObject productParamOld = com.alibaba.fastjson.JSONObject.parseObject(row.getProductParam());
                if (null == productParamOld) {
                    productParamOld = new com.alibaba.fastjson.JSONObject();
                }

                //if (priceSaleType == 2)
                productParamOld.put("payPrice1", getDiyProductrBuyPrice(row.getProductCode(), 1, PriceTypeEnum.MONTH.getFlag()));
                productParamOld.put("payPrice3", getDiyProductrBuyPrice(row.getProductCode(), 3, PriceTypeEnum.MONTH.getFlag()));
                productParamOld.put("payPrice6", getDiyProductrBuyPrice(row.getProductCode(), 6, PriceTypeEnum.MONTH.getFlag()));
                productParamOld.put("payPrice12", getDiyProductrBuyPrice(row.getProductCode(), 1, PriceTypeEnum.YEAR.getFlag()));

                row.setProductParam(com.alibaba.fastjson.JSONObject.toJSONString(productParamOld));

            });
        }

        String code = sysProductUtils.getErrCode();
        if (list == null) {
            return XmlCodeParser.formatResultTips(false, code);
        }

        Map<String, Object> cdt = new HashMap<>();
        cdt.put("productClass", productClass);
        if (!StrTool.isBlankStr(subClass)) {
            cdt.put("subClass", subClass);
        }
        if (!StrTool.isBlankStr(productCode)) {
            cdt.put("productCode", productCode);
        }
        cdt.put("status", ProductStatusEnum.NORMAL.getStatus());
        if (recommend > -1) {
            cdt.put("recommend", recommend);
        }
        if (!"".equals(productName)) {
            cdt.put("productName", productName);
        }
        int count = sysProductUtils.getProductRows(cdt);
        int pages = Math.round((count + pageSize - 1) / pageSize);

        String ret = XmlCodeParser.formatResultPageData(code, page, pages, pageSize, count, CJsonTool.listBeanToJson(list));
        if (!StrTool.isBlankStr(subClass)) {
            ret = JsonTool.addNode(ret, "category", subClassInfo);
        }
        return ret;
    }


    private int getDiyProductrBuyPrice(String productCode, int applyTime, String priceType){
        int price = 0;
        Map<String, Object> cdt = new HashMap<>();
        cdt.put("productCode", productCode);
        cdt.put("applyTime", applyTime);
        cdt.put("priceType", priceType);
        cdt.put("num", 1);
        cdt.put("chargeId", 1);

        DicPriceDiyUtils dicPriceDiyUtils = new DicPriceDiyUtils();
        com.alibaba.fastjson.JSONObject priceJson = com.alibaba.fastjson.JSONObject.parseObject(dicPriceDiyUtils.getPrice(cdt));
        dicPriceDiyUtils.dispose();
        if (null != priceJson && priceJson.containsKey("data")) {
            price = priceJson.getJSONObject("data").getInteger("payPrice");
        }
        return price;
    }

    /**
     * 获取产品列表(管理员)
     *
     * @param req
     * @param resp
     * @param p={"productClass":"productClass"}
     * @param sid
     * @return
     */
    public String getProductList(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        String productClass = JsonTool.getStrValue(p, "productClass", "");
        String regType = JsonTool.getStrValue(p, "regType", "");
        String productName = JsonTool.getStrValue(p, "productName", "");
        String productCode = JsonTool.getStrValue(p, "productCode", "");
        String status = JsonTool.getStrValue(p, "status", "");
        int page = JsonTool.getIntValue(p, "page", 1);
        int pageSize = JsonTool.getIntValue(p, "pageSize", CConsts.PAGE_SIZE);
        String subClass = JsonTool.getStrValue(p, "subClass", "");

        Map<String, Object> filter = new HashedMap();
        if (!StrTool.isBlankStr(productName)) {
            filter.put("productName", productName);
        }
        if (!StrTool.isBlankStr(productCode)) {
            filter.put("productCode", productCode);
        }
        if (!StrTool.isBlankStr(status)) {
            filter.put("status", status);
        }

        SysProductUtils sysProductUtils = new SysProductUtils();
        List<SysProductView> list = sysProductUtils.getProductList(productClass, regType, page, pageSize, filter, subClass);
        String code = sysProductUtils.getErrCode();
        if (list == null) {
            return XmlCodeParser.formatResultTips(false, code);
        }

        Map<String, Object> cdt = new HashMap<String, Object>();
        cdt.put("productClass", productClass);
        if (!StrTool.isBlankStr(regType)) {
            cdt.put("regType", regType);
        }
        int count = sysProductUtils.getProductRows(cdt, filter);
        int pages = Math.round((count + pageSize - 1) / pageSize);

        return XmlCodeParser.formatResultPageData(code, page, pages, pageSize, count, JsonTool.listBeanToJson(list));
    }

    /**
     * 添加产品
     *
     * @param req
     * @param resp
     * @param p={"productClass":"productClass","productCode":"productCode","productName":"productName"}
     * @param sid
     * @return
     */
    public String addSysProduct(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        //产品类型
        String productClass = JsonTool.getStrValue(p, "productClass", "");
        //产品编号
        String productCode = JsonTool.getStrValue(p, "productCode", "");
        //产品名称
        String productName = JsonTool.getStrValue(p, "productName", "");
        Integer recommend = JsonTool.getIntValue(p, "recommend", 0);
        //子分类
        String subClass = JsonTool.getStrValue(p, "subClass", "");
        if (StrTool.isBlankStr(productCode) || StrTool.isBlankStr(productName)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }
        String regType = JsonTool.getStrValue(p, "regType", "");

        SysProduct det = new SysProduct();
        det.setProductClass(productClass);
        det.setProductCode(productCode);
        det.setProductName(productName);
        det.setSubClass(subClass);
        det.setRecommend(recommend);
        det.setRegType(regType);
        SysProductUtils sysProductUtils = new SysProductUtils();
        int ret = sysProductUtils.addSysProduct(det);

        //同步更新到代理
        SysProductAgentHelper.mustAddPro(productClass, productCode, subClass);

        return XmlCodeParser.formatResultTips(ret > 0, sysProductUtils.getErrCode());
    }

    /**
     * 更新产品信息
     *
     * @param req
     * @param resp
     * @param p={""}
     * @param sid
     * @return
     */
    public String uptSysProduct(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        String productClass = JsonTool.getStrValue(p, "productClass", "");
        //产品编号
        String productCode = JsonTool.getStrValue(p, "productCode", "");
        //产品名称
        String productName = JsonTool.getStrValue(p, "productName", "");
        /**
         * 产品状态
         */
        String status = JsonTool.getStrValue(p, "status", "");
        //排序
        long listId = JsonTool.getLongValue(p, "listId", 0);
        String subClass = JsonTool.getStrValue(p, "subClass", "");
        Integer recommend = JsonTool.getIntValue(p, "recommend", 0);


        if (StrTool.isBlankStr(productCode) || StrTool.isBlankStr(productName) || StrTool.isBlankStr(productClass)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }
        //修改内容
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("productName", productName);
        map.put("listId", listId);
        map.put("recommend", recommend);
        if (!StrTool.isBlankStr(status)) {
            map.put("status", status);
        }
        if (!StrTool.isBlankStr(subClass)) {
            map.put("subClass", subClass);
        }

        //条件
        Map<String, Object> mapCod = new HashMap<String, Object>();
        mapCod.put("productCode", productCode);
        mapCod.put("productClass", productClass);

        SysProductUtils sysProductUtils = new SysProductUtils();
        int ret = sysProductUtils.modSysProduct(map, mapCod);
        String code = sysProductUtils.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }

        //强制更新下级代理数据
        SysProductAgentHelper.mustUpdateProductDetail(ProductTypeEnum.getByTag(productClass), productCode, subClass);

        return XmlCodeParser.formatResultTips(ret > 0, code);
    }

    /**
     * 删除产品
     *
     * @param req
     * @param resp
     * @param p={"productCode":"productCode"}
     * @param sid
     * @return
     */
    public String delSysProduct(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        //产品编号
        String productClass = JsonTool.getStrValue(p, "productClass", "");
        String productCode = JsonTool.getStrValue(p, "productCode", "");
        String subClass = JsonTool.getStrValue(p, "subClass", "");
        if (StrTool.isBlankStr(productCode) || StrTool.isBlankStr(productClass)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("productClass", productClass);
        map.put("productCode", productCode);
        if (!StrTool.isBlankStr(subClass)) {
            map.put("subClass", subClass);
        }
        SysProductUtils sysProductUtils = new SysProductUtils();
        int ret = sysProductUtils.delSysProduct(map);
        String code = sysProductUtils.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }

        //强制更新到下级
        SysProductAgentHelper.mustDelPro(productClass, productCode, subClass);

        return XmlCodeParser.formatResultTips(ret > 0, code);
    }

    /**
     * 删除域名按注册商设置的价格
     *
     * @param req
     * @param resp
     * @param p={"productCode":"productCode"}
     * @param sid
     * @return
     */
    public String delSysDomainRegTypePrice(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        //产品编号
        String productCode = JsonTool.getStrValue(p, "productCode", "");
        if (StrTool.isBlankStr(productCode)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("productCode", productCode);
        SysProductUtils sysProductUtils = new SysProductUtils();
        int ret = sysProductUtils.delSysProduct(map);
        String code = sysProductUtils.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }
        return XmlCodeParser.formatResultTips(ret > 0, code);
    }

    /**
     * 自定义产品保存.
     *
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String saveDiyProductSet(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        JSONArray product = CJsonTool.getJsonArray(p, "product", new JSONArray());
        JSONArray priceItem = CJsonTool.getJsonArray(p, "priceItem", new JSONArray());
        if (product.isEmpty() || (priceItem.isEmpty() && CloudContextUtils.getAgentId() == 0)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        SysProductUtils utils = new SysProductUtils();
        int ret = utils.saveProductSet((JSONObject) product.get(0), priceItem, ProductTypeEnum.DIY);
        String code = utils.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }
        return XmlCodeParser.formatResultTips(ret > 0, code);
    }

    /**
     * 保存域名产品设置
     *
     * @param req
     * @param resp
     * @param p={}
     * @param sid
     * @return
     */
    public String delDomainProductByRegTypeSetPrice(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        String productCode = JsonTool.getStrValue(p, "productCode", "");        //产品数据项
        String productClass = JsonTool.getStrValue(p, "productClass", "");        //产品数据项

        if (StrTool.isBlankStr(productCode) || StrTool.isBlankStr(productClass)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        SysProductUtils utils = new SysProductUtils();
        int ret = utils.delDomainProductSetPriceByRegType(productClass, productCode);
        String code = utils.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }
        return XmlCodeParser.formatResultTips(ret > 0, code);
    }

    /**
     * 获取产品默认价格
     *
     * @param req
     * @param resp
     * @param p={}
     * @param sid
     * @return
     */
    public String getProDefaultPrice(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        //产品类型
        String productClass = JsonTool.getStrValue(p, "productclass", "");

        //产品价格列表
        SysProductUtils spUtils = new SysProductUtils();
        Map<String, Object> cdt = new HashMap<String, Object>();
        if (!StrTool.isBlankStr(productClass)) {
            cdt.put("productclass", productClass);
        }
        List<SysProductView> list = spUtils.getProDefaultPrice(cdt);
        String code = spUtils.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(list));
    }

    /**
     * 获取产品可购买时长
     *
     * @param req
     * @param resp
     * @param p={}
     * @param sid
     * @return
     */
    public String getProductBuyTime(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        int uid = CloudContextUtils.getSession().getUID();

        String productCode = JsonTool.getStrValue(p, "productCode", "");
        String chargeId = JsonTool.getStrValue(p, "chargeId", "");
        Map<String, Object> cdt = new HashMap<String, Object>();
        cdt.put("productCode", productCode);
        cdt.put("chargeId", chargeId);
        cdt.put("userId", uid);
        SysProductPriceUtils priceUtils = new SysProductPriceUtils();
        List<Integer> list = priceUtils.getProductBuyTime(cdt);
        String code = priceUtils.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }

        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(list));
    }

    /**
     * 获取促销时长
     *
     * @param req
     * @param resp
     * @param p={}
     * @param sid
     * @return
     */
    public String getPromotionTime(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        int uid = CloudContextUtils.getSession().getUID();
        String productCode = JsonTool.getStrValue(p, "productCode", "");
        int chargeId = JsonTool.getIntValue(p, "chargeId", 1);
        String regType = JsonTool.getStrValue(p, "regType", "");
        String instanceId = JsonTool.getStrValue(p, "instanceId", "");

        SysPromotionUtils promotionUtils = new SysPromotionUtils();
        Map<String, Object> param = new HashMap<>();
        param.put("productCode", productCode);
        param.put("chargeId", chargeId);
        param.put("agentId", CloudContextUtils.DEF_AGENT_ID);

        List<SysPromotion> promotionList = new ArrayList<>();

        //虚机、自定义产品指定续费价格处理 2019-11-12 hl831
        if (chargeId == 2 && (!StrTool.isBlankStr(regType) && !StrTool.isBlankStr(instanceId)) && (ProductTypeEnum.V_HOST.getTag().equals(regType) || ProductTypeEnum.DIY.getTag().equals(regType))) {
            promotionList = promotionUtils.getSysPromotionList(productCode,Integer.valueOf(instanceId), chargeId);
            if (promotionList.size() > 0) {
                return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(promotionList));
            }
        }
        //云产品指定续费价格处理 2019-10-21 hl831
        SysProductUtils sysProductUtils = new SysProductUtils();
        SysProduct sp = sysProductUtils.queryProductByProductCode(productCode);
        //获取价格设置方式

        //合并主平台产品基础数据
        if (CloudContextUtils.getAgentId() > 0) {
            SysProductView viewPlat = sysProductUtils.querySysProductAppointAgent(sp.getProductClass(), productCode,
                    sp.getRegType(), CloudContextUtils.DEF_AGENT_ID);
            JSONObject paramPlat = JSONObject.fromObject(viewPlat.getProductParam());
            JSONObject sysProductParam = JSONObject.fromObject(sp.getProductParam());
            paramPlat.putAll(sysProductParam);
            sp.setProductParam(paramPlat.toString());
        }

        JSONObject productParam = sp == null ? new JSONObject() : JSONObject.fromObject(sp.getProductParam());
        int priceSaleType = null != productParam && productParam.containsKey("saleType") ? productParam.getInt("saleType") : 1;

        String code = SysErr.CODE_SUCCESS;

        if (priceSaleType == 2){
            param.put("userId", uid);
            SysDiyProductPriceUtils sysDiyProductPriceUtils = new SysDiyProductPriceUtils();
            promotionList = sysDiyProductPriceUtils.getDiyProductBuyTime(param);
            code = sysDiyProductPriceUtils.getErrCode();
            sysDiyProductPriceUtils.dispose();
        } else {
            promotionList = promotionUtils.getSysPromotionList(param);
            code = promotionUtils.getErrCode();
        }

        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }


        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(promotionList));
    }

    /**
     * 获取云产品信息
     *
     * @param req
     * @param resp
     * @param p={"productCode":"productCode"}
     * @param sid
     * @return
     */
    private String getProduct(HttpServletRequest req, HttpServletResponse resp, String p, String sid, boolean isBuyProduct) {
        String productCode = JsonTool.getStrValue(p, "productCode", "");    //产品编号
        String regType = JsonTool.getStrValue(p, "regType", "");            //注册商
        int chargeId = JsonTool.getIntValue(p, "chargeId", 0);           //价格项
        String productClass = JsonTool.getStrValue(p, "productClass", "cloud"); //服务类型

        Map<String, Object> cdt = new HashMap<>();
        cdt.put("productClass", productClass);
        if (!StrTool.isBlankStr(productCode)) {
            cdt.put("productCode", productCode);
        }
        if (!StrTool.isBlankStr(regType)) {
            cdt.put("regType", regType);
        }

        //产品基础信息
        SysProductUtils sysProductUtils = new SysProductUtils();
        SysProductView productView = sysProductUtils.querySysProduct(cdt);
        String code = sysProductUtils.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }

        //获取主平台产品设置
        cdt.put("sp.agentId", CloudContextUtils.DEF_AGENT_ID);
        SysProductView productViewPlat = sysProductUtils.querySysProduct(cdt);


        com.alibaba.fastjson.JSONObject productParamPlat = com.alibaba.fastjson.JSONObject.parseObject(productViewPlat.getProductParam());
        com.alibaba.fastjson.JSONObject productParam = com.alibaba.fastjson.JSONObject.parseObject(productView.getProductParam());
        if (null != productParam) {
            if (null == productParamPlat) {
                productParamPlat = new com.alibaba.fastjson.JSONObject();
            }
            productParamPlat.putAll(productParam);
        }
        productView.setProductParam(com.alibaba.fastjson.JSONObject.toJSONString(productParamPlat));

        //预装软件参数
        String addInitSoft = (null != productParam && productParam.containsKey("addInitSoft")) ? productParam.getString("addInitSoft") : "N";
        String webSoft = (null != productParam && productParam.containsKey("webSoft")) ? productParam.getString("webSoft") : "";

        List<DicParamItem> softList = new ArrayList<>();
        if ("Y".equals(addInitSoft)){
            DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
            softList = dicParamItemUtils.getVHostSupportWebSoft(webSoft.split(","));
            dicParamItemUtils.dispose();
        }

        //价格设置方式 1 - 会员折扣  2 - 按会员等级设置
        int saleType = null != productParam && productParam.containsKey("saleType") ? productParam.getInteger("saleType") : 1;

        //产品促销列表
        SysPromotionUtils promotionUtils = new SysPromotionUtils();
        SysDiyProductPriceUtils sysDiyProductPriceUtils = new SysDiyProductPriceUtils();
        List<SysPromotion> promotionList = null;
        List<SysDiyProductPriceBean> sysDiyProductPriceBeans = null;

        if (productView != null) {
            Map<String, Object> param = new HashMap<>();
            param.put("productCode", productView.getProductCode());
            if (chargeId > 0) {
                param.put("chargeId", chargeId);
            }
            param.put("agentId", CloudContextUtils.DEF_AGENT_ID);

            if (saleType == 2){
                if (isBuyProduct){
                    param.put("levelCode", DicParamUtils.getParamValue("showPriceLevel"));
                    param.put("userId", CloudContextUtils.getSession().getUID());
                    promotionList = sysDiyProductPriceUtils.getDiyProductBuyTime(param);
                } else {
                    sysDiyProductPriceBeans = sysDiyProductPriceUtils.querySysDiyProductPrice(param, "", 0, 0);
                }
            }else{
                promotionList = promotionUtils.getSysPromotionList(param);
            }
        }
        sysDiyProductPriceUtils.dispose();


        String promotionListStr = "";
        if (saleType == 2 && !isBuyProduct) {
            promotionListStr = YJsonTool.listBeanToJson(sysDiyProductPriceBeans);
        }else{
            promotionListStr = JsonTool.listBeanToJson(promotionList);
        }

        String productViewStr = CJsonTool.beanToJson(productView);
        //if (isBuyProduct){
            productViewStr = CJsonTool.addNode(productViewStr, "addInitSoft", addInitSoft);
            productViewStr = CJsonTool.addNode(productViewStr, "softList", CJsonTool.listBeanToJson(softList));
        //}

        String data = String.format("{\"productView\":%s,\"promotionList\":%s}", productViewStr, promotionListStr);
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, data);
    }

    /**
     * 根据字段查询所有符合条件的数据
     *
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String queryProductClass(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        String productClass = JsonTool.getStrValue(p, "productclass", "");//产品类型
        if (StrTool.isBlankStr(productClass)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }
        SysProductUtils spUtils = new SysProductUtils();
        List<SysProduct> queryProductClass = spUtils.queryProductClass(productClass);
        String code = spUtils.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code);
        }
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(queryProductClass));
    }

    /**
     * 同步产品
     *
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String syncSysProduct(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        SysProductUtils utils = new SysProductUtils();
        utils.syncAgentWiniisVHostProduct();
        utils.syncAgentDomainProduct();
        utils.syncAgentDiyProduct();
        utils.syncAgentProductDetailAndStatus(CloudContextUtils.getAgentId());
        utils.dispose();

        //强制更新产品说明
        Integer syncProDetail = JsonTool.getIntValue(p, "syncProDetail", 0);
        if (1 == syncProDetail) {
            SysProductAgentHelper.agentUpdateProductDetailAll();
        }

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 获取成本价
     *
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String getAgentDiyProductCostPrice(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        int uid = CloudContextUtils.getSession().getUID();
        String productClass = JsonTool.getStrValue(p, "productClass", "diy");
        String productCode = JsonTool.getStrValue(p, "productCode", "");
        if (StrTool.isBlankStr(productClass) || StrTool.isBlankStr(productCode)){
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }
        //1.先获取产品
        SysProductUtils utils = new SysProductUtils();
        SysProductView product = utils.querySysProduct(productClass, productCode);
        String code  = utils.getErrCode();
        String msg  = utils.getMsg();
        utils.dispose();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            return XmlCodeParser.formatResultTips(false, code, msg);
        }

        if (null == product){
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_DATA_NOT_FOUND);
        }
        if (CloudContextUtils.getAgentId() > 0){
            product = DicPriceDiyUtils.agentProductMergeSysProductParam(product);
        }

        String levelCode = DicPriceDiyUtils.getAgentUserLevelCode();
        int saleType = DicPriceDiyUtils.getProductPriceSetType(product);


        //产品促销列表
        SysPromotionUtils promotionUtils = new SysPromotionUtils();
        SysDiyProductPriceUtils sysDiyProductPriceUtils = new SysDiyProductPriceUtils();

        List<SysPromotion> addPromotionList = new ArrayList<>();
        List<SysPromotion> renewPromotionList = new ArrayList<>();
        if (product != null) {
            Map<String, Object> param = new HashMap<>();
            param.put("productCode", product.getProductCode());
            param.put("agentId", CloudContextUtils.DEF_AGENT_ID);
            param.put("chargeId", 1);
            if (saleType == 2){
                param.put("levelCode", levelCode);
                param.put("userId", CloudContextUtils.getAgentId());
                addPromotionList = sysDiyProductPriceUtils.getDiyProductBuyTime(param);
            }else{
                addPromotionList = promotionUtils.getSysPromotionList(param);
            }


            param.put("chargeId", 2);
            if (saleType == 2){
                param.put("levelCode", levelCode);
                param.put("userId", CloudContextUtils.getAgentId());
                renewPromotionList = sysDiyProductPriceUtils.getDiyProductBuyTime(param);
            }else{
                renewPromotionList = promotionUtils.getSysPromotionList(param);
            }
        }
        sysDiyProductPriceUtils.dispose();

        Map<String, Object> cdt = new HashMap<>();
        DicPriceDiyUtils dicPriceDiyUtils = new DicPriceDiyUtils();
        for (SysPromotion obj: addPromotionList){
            cdt.clear();
            cdt.put("productCode", productCode);
            cdt.put("productClass", productClass);
            cdt.put("priceType", obj.getApplyType());
            cdt.put("applyTime", obj.getApplyTime());
            cdt.put("num", 1);
            String prices = dicPriceDiyUtils.getAddPrice(cdt);
            String price = JsonTool.getStrValue(prices, "payPrice", "0.0");
            obj.setPlusValue2(price);

        }
        for (SysPromotion obj: renewPromotionList){
            cdt.clear();
            cdt.put("productCode", productCode);
            cdt.put("productClass", productClass);
            cdt.put("priceType", obj.getApplyType());
            cdt.put("applyTime", obj.getApplyTime());
            cdt.put("num", 1);
            String prices = dicPriceDiyUtils.getRenewPrice(cdt);
            String price = JsonTool.getStrValue(prices, "payPrice", "0.0");
            obj.setPlusValue2(price);

        }

        String data = String.format("{\"addPromotionList\":%s,\"renewPromotionList\":%s}", JsonTool.listBeanToJson(addPromotionList), JsonTool.listBeanToJson(renewPromotionList));

        //2.获取产品可销售的价格

        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, data);
    }

    /**
     * 获取产品信息通用接口.
     */
    public String getProductPublic (HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
        return this.getProduct(req, resp, p, sid, false);
    }
}
